package com.interesting.client.UI.home;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.interesting.R;
import com.interesting.base.core.BaseActivity;
import com.interesting.base.http.HttpUtil;
import com.interesting.client.UI.commonActivity.UIUtil;
import com.interesting.client.UI.setting.LoginActivity;
import com.interesting.client.application.AppHolder;
import com.interesting.client.bean.Emotions;

/** 发表评论 */
public class CommentMesgActivity extends BaseActivity {
	String tag = "comment";
	private ImageView send;
	private EditText content;
	private String mesgId;
	private GridView blog_sendmsg_gvemotion;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getSupportActionBar().setCustomView(R.layout.text_actionbar_custom_view);
		setTitle(getResources().getString(R.string.review));

		setContentView(R.layout.commit_message_activity);

		mesgId = getIntent().getExtras().getString("mesgId");

		send = (ImageView) findViewById(R.id.home_commit_message_submit);
		content = (EditText) findViewById(R.id.home_commit_message_content);
		blog_sendmsg_gvemotion = (GridView) findViewById(R.id.blog_sendmsg_gvemotion);
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {

				case R.id.home_commit_message_submit: // 提交
					String userId = AppHolder.getInsatnce().user.getUserId();
					if (UIUtil.isLoggedIn()) {
						toSend(userId, content.getText().toString());
					} else {
						UIUtil.showLongToast(getApplicationContext(), "请登录后在评论...");
						Intent toLogin = new Intent(CommentMesgActivity.this, LoginActivity.class);
						startActivity(toLogin);
					}
					break;
				}
			}
		};
		send.setOnClickListener(listener);
	}

	private void toSend(String userId, String text) {
		if (text.length() < getResources().getInteger(R.integer.text_minCount)) { // 文字数目最少6个，
																					// 进行提示
			UIUtil.showToast(getApplicationContext(), "说的忒少，求喷");
		} else {// 无图片
			UIUtil.showWaitDialog(getSupportFragmentManager());

			new SendCommentAsync().execute(userId, text);
		}
	}

	/** 发表评论 */
	private class SendCommentAsync extends AsyncTask<String, Void, JSONObject> {

		@Override
		protected JSONObject doInBackground(String... params) {
			Map<String, String> request = new HashMap<String, String>();
			request.put("act", "comments");
			request.put("userId", params[0]);
			request.put("targetId", mesgId);
			request.put("content", params[1]);
			return HttpUtil.parseResult(HttpUtil.post(request));
		}

		@Override
		protected void onPostExecute(JSONObject result) {
			if (result != null) {

				// Comment item = new Comment();
				// item.setUserId(AppHolder.getInsatnce().user.getUserId());
				// item.setUserName(AppHolder.getInsatnce().user.getUserName());
				// commentdata.add(item);
				// commentAdapter.notifyDataSetChanged();
			}
			UIUtil.hideDialog(getSupportFragmentManager());
			finish();
			super.onPostExecute(result);
		}
	}

	public void addexpression(View view) {
		if (blog_sendmsg_gvemotion.getVisibility() == View.GONE) {
			blog_sendmsg_gvemotion.setVisibility(View.VISIBLE);

			ArrayList<Emotions> emotionList = new ArrayList<Emotions>();
			ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();

			for (int i = 0; i < 70; i++) {
				Emotions emtions = emotionList.get(i);
				if (emtions != null) {
					HashMap<String, Object> map = new HashMap<String, Object>();

					Field f;
					try {
						f = (Field) R.drawable.class.getDeclaredField(emtions.getImageName());
						int j = f.getInt(R.drawable.class);
						map.put("ItemImage", j);// 添加图像资源的ID
						lstImageItem.add(map);
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (NoSuchFieldException e) {
						e.printStackTrace();
					} catch (IllegalArgumentException e) {
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					}

				}

			}

			// 生成适配器的ImageItem <====> 动态数组的元素，两者一一对应
			SimpleAdapter saImageItems = new SimpleAdapter(this, lstImageItem,// 数据来源
					R.layout.gridview_emotion_item,
					// 动态数组与ImageItem对应的子项
					new String[] { "ItemImage" },

					// ImageItem的XML文件里面的一个ImageView
					new int[] { R.id.blog_sendmsg_emotion });

			blog_sendmsg_gvemotion.setAdapter(saImageItems);
		} else {
			blog_sendmsg_gvemotion.setVisibility(View.GONE);
		}

	}
}
